package JUnit;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import Servlets.WhatsAppConstatnts;
import protocol.Headers;

public class List extends mainTest{
	@Before
	public void setUp() throws Exception {
		_url = "list";
	}
	
	@Test
	public void test405() {
		String response1 = this
				.runtest(new RunnableClient(
						"localhost",
						9969,
						"GET /"
								+ _url
								+ ".jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		assertTrue(response1.contains("HTTP/1.1 " + Headers.STATUS_405));
	}

	@Test
	public void test403() {
		String response1 = this
				.runtest(new RunnableClient(
						"localhost",
						9969,
						"POST /"
								+ _url
								+ ".jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		assertTrue(response1.contains(Headers.STATUS_403));
	}
	@Test
	public void testERROR_273() {
		String response1 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		String cookie = this.getCookie(response1);
		String response2 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /" + _url + ".jsp HTTP/1.1\nCookie: " + cookie
						+ "\n\nType=yochailehman2&Target=0546861622\n$"));

		assertTrue(response2.contains(WhatsAppConstatnts.ERROR_273));
	}
	
	@Test
	public void testERROR_777() {
		String response1 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		String cookie = this.getCookie(response1);
		String response2 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /" + _url + ".jsp HTTP/1.1\nCookie: " + cookie
						+ "\n\nList=ggggg\n$"));

		assertTrue(response2.contains(WhatsAppConstatnts.ERROR_777));
	}
	
	@Test
	public void testUsers() {
		String response5 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=userToAdd&Phone=123456\n$"));
		String cookie2 = this.getCookie(response5);
		String response1 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		String cookie = this.getCookie(response1);
		String response3 = this
				.runtest(new RunnableClient(
						"localhost",
						9969,
						"POST /send.jsp HTTP/1.1\nCookie: "
								+ cookie2
								+ "\n\nType=Direct&Target=0546861622&Content=gggggggggg\n$"));
		String response4 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /" + _url + ".jsp HTTP/1.1\nCookie: " + cookie
						+ "\n\nList=Users\n$"));
		System.out.println(response4.toString());
		assertTrue(response4.toString().contains("yochailehman2@0546861622\nuserToAdd@123456") || response4.toString().contains("userToAdd@123456\nyochailehman2@0546861622"));
	}
	
	@Test
	public void testGroup() {
		String response5 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=userToAdd&Phone=123456\n$"));
		this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehmgan2&Phone=05468612622\n$"));
		String cookie2 = this.getCookie(response5);
		this.runtest(new RunnableClient("localhost", 9969,
				"POST /create_group.jsp HTTP/1.1\nCookie: " + cookie2 + "\n\n"
						+ WhatsAppConstatnts.GROUP_NAME
						+ "=TestGroup1&Users=userToAdd,yochailehman2\n$"));
		this.runtest(new RunnableClient("localhost", 9969,
				"POST /create_group.jsp HTTP/1.1\nCookie: " + cookie2 + "\n\n"
						+ WhatsAppConstatnts.GROUP_NAME
						+ "=TestGroup2&Users=userToAdd,yochailehman2,yochailehmgan2\n$"));
		String response4 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /" + _url + ".jsp HTTP/1.1\nCookie: " + cookie2
						+ "\n\nList=TestGroup2\n$"));
		assertTrue(response4.contains("userToAdd,yochailehman2,yochailehmgan2"));
		String response6 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /" + _url + ".jsp HTTP/1.1\nCookie: " + cookie2
						+ "\n\nList=TestGroup1\n$"));
		assertTrue(response6.contains("userToAdd,yochailehman2"));
		assertFalse(response6.contains("userToAdd,yochailehman2,yochailehmgan2"));
	}
	
	@Test	
	public void testGroups() {
		String response5 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=userToAdd&Phone=123456\n$"));
		this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehmgan2&Phone=05468612622\n$"));
		String cookie2 = this.getCookie(response5);
		this.runtest(new RunnableClient("localhost", 9969,
				"POST /create_group.jsp HTTP/1.1\nCookie: " + cookie2 + "\n\n"
						+ WhatsAppConstatnts.GROUP_NAME
						+ "=TestGroup1&Users=userToAdd,yochailehman2\n$"));
		this.runtest(new RunnableClient("localhost", 9969,
				"POST /create_group.jsp HTTP/1.1\nCookie: " + cookie2 + "\n\n"
						+ WhatsAppConstatnts.GROUP_NAME
						+ "=TestGroup2&Users=userToAdd,yochailehman2,yochailehmgan2\n$"));
		String response6 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /" + _url + ".jsp HTTP/1.1\nCookie: " + cookie2
						+ "\n\nList=Groups\n$"));

		assertTrue(response6.contains("TestGroup2: 05468612622,0546861622,123456\nTestGroup1: 0546861622,12345"));
	}



}
